package cn.zxcoder.controller.goods;

import cn.zxcoder.entity.goods.Product;
import cn.zxcoder.entity.Res;
import cn.zxcoder.service.IImageService;
import cn.zxcoder.service.IProductService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "获取商品信息-api")
@RequestMapping("/api/Goods")
@RestController
@Slf4j
public class ProductListController {

    @Autowired
    IProductService productServiceImpl;

    @Resource
    RedisTemplate redisTemplate;

    @Autowired
    IImageService iImageServiceIml;
    /**
     * 给前端返回整个数据库的商品信息
     *
     * @return 整个数据库的商品信息
     */
    @ApiOperation("获取商品信息")
    @PostMapping("/list")
    @Transactional(readOnly = true)
    public Res goodsList(HttpServletRequest request, @RequestParam String page, @RequestParam String limit, @RequestParam String searchName) throws IOException {
        long startTime = System.currentTimeMillis();
        redisTemplate.opsForValue().increment("product:count");

        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        if (!searchName.equals("")) {
            productQueryWrapper.like("goodsName", searchName);

        }
//        if (true) {
//            throw new NullPointerException();
//        }
        IPage<Product> ipage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        IPage<Product> page1 = productServiceImpl.page(ipage,productQueryWrapper);
        for (Product record : page1.getRecords()) {
            String srcLists = record.getSrcLists();
            if (srcLists.equals("")) {
                continue;
            }
            ArrayList<String> list = new ArrayList<>();
            String[] split = srcLists.split(",");

            for (String s : split) {
                String imgUrl = iImageServiceIml.getById(Integer.parseInt(s)).getImgUrl();
                if (imgUrl != null && !imgUrl.equals("")) {
                    list.add(imgUrl);
                }
            }
            record.setSrcList(list);
        }

        String result = JSON.toJSONString(page1);


        if (page1 != null) {
            return new Res(true, result, "查询成功");
        } else {
            return new Res(false, null, "查询失败");
        }


    }

    /**
     * 删除一个商品信息
     *
     * @return 删除后的信息
     */
    @ApiOperation("删除商品信息")
    @DeleteMapping("/delete/{ids}")
    @Transactional
    public Res goodsDelete(@PathVariable String ids) throws IOException {
        String[] split = ids.trim().split(",");
        for (String ids1 : split) {
            boolean b = productServiceImpl.removeById(Integer.parseInt(ids1));
            if (!b) {
                return new Res(b, null, "删除失败");
            }
        }
            return new Res(true, null, "删除成功");
    }





}
